//
//  WalletTabViewController.m
//  TenMinDemo
//
//  Created by Shawn Li on 2021/3/8.
//  Copyright © 2021 CYXiang. All rights reserved.
//

#import "WalletTabViewController.h"
#import "WalletTabHeader.h"
#import "WalletTableViewCell.h"
#import "CoinDetailViewController.h"
#import "CYXBaseRequest.h"
#import "CoinItemModel.h"
#import "TransferRecordViewController.h"

@interface WalletTabViewController ()
/// <#注释#>
@property (nonatomic, strong) WalletTabHeader *header;
/// <#注释#>
@property (nonatomic, assign) BOOL isHideSmall;
/// <#注释#>
@property (nonatomic, copy) NSString *keyword;

/// <#注释#>
@property (nonatomic, strong) NSURLSessionDataTask *task;

/// <#注释#>
@property (nonatomic, strong) NSArray *showArr;
@end

@implementation WalletTabViewController

- (instancetype)init{
    // 设置tableView的分组样式为Group样式
    return [self initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (!kAppDelegate.walletM) {
        [self getUserAssets];
    }else{
        [self configwalletAndRefresh];
    }
    
    [LCNotifiCenter addObserver:(self) selector:(@selector(configwalletAndRefresh)) name:(@"WalletModelSuccess") object:nil];
    
}
- (void)setupUI{
    [super setupUI];
    self.title = LCLocalString(@"wallet");
    self.tableView.isNeedEmpty = @"1";
    self.isHideTitle = NO;
    [self setRightNavItem:LCLocalString(@"all_jy") font:[UIFont boldSystemFontOfSize:12] color:[UIColor whiteColor] icon:nil];
    
    WalletTabHeader *header = [[WalletTabHeader alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 195)];
    LRWeakSelf(self);
    header.actionBlock = ^(BOOL isHideSmall) {
        weakself.isHideSmall = isHideSmall;
        [weakself configwalletAndRefresh];
    };
    
    header.keywordDidChangeBlock = ^(NSString * _Nonnull keyword) {
        weakself.keyword = keyword;
//        [weakself getUserAssets];
        [weakself configwalletAndRefresh];
    };
    
    self.header = header;
    self.tableView.tableHeaderView = self.header;
    self.tableView.backgroundColor = [UIColor colorWithRed:(23.0)/255 green:(25.0)/255 blue:(29.0)/255 alpha:1];
    [self.tableView registerNib:[UINib nibWithNibName:@"WalletTableViewCell" bundle:nil] forCellReuseIdentifier:@"WalletTableViewCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [LCNotifiCenter addObserver:(self) selector:(@selector(logOnNewUser)) name:(@"UserLogOnSuccess") object:nil];
    [LCNotifiCenter addObserver:(self) selector:(@selector(resetUserInfo)) name:(@"UserLogOutSuccess") object:nil];
     
}

- (void)resetUserInfo{
    kAppDelegate.walletM = nil;
    [self.tableView reloadData];
}

- (void)logOnNewUser{
    kAppDelegate.walletM = nil;
    [self.tableView reloadData];
    [self getUserAssets];
   
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    if (!self.walletM) {
        [self getUserAssets];
//    }else{
//        [self.header refreshWith:self.walletM];
//    }
}

- (void)rightNavItemClick:(id)sender{
    TransferRecordViewController *vc = LoadVCFormXib(TransferRecordViewController);
    vc.listType = 0;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.showArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 73;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CoinItemModel *model = self.showArr[indexPath.row];
    static NSString *ID1 = @"WalletTableViewCell";
    WalletTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID1];
    [cell refreshWith:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CoinItemModel *item =  self.showArr[indexPath.row];
    CoinDetailViewController *vc =  LoadVCFormXib(CoinDetailViewController);
    vc.coinM = item;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark-

- (void)getUserAssets{
//    if (self.task) {
//        [self.task cancel];
//    }
    
    self.task = [CYXBaseRequest postWithUrl:API_getUserAssets param:nil resultClass:[WalletMainModel class] success:^(id res) {
        if ([res isEqual: RequestErrorCode]) {

        }else{
            kAppDelegate.walletM = res;
            LCPostNotification(@"WalletModelSuccess");
//            [self walletDidUpdate];
        }

        CYXLog(@"%@",res)
    } failure:^(NSError *error) {
        CYXLog(@"%@",error)
    }];
}

- (void)configwalletAndRefresh{
    [self configShowArr];
    [self refreshWallet];
}

- (void)refreshWallet{
    [self.header refreshWith:kAppDelegate.walletM];
    [self.tableView reloadData];
}

- (void)configShowArr{
    if (!kAppDelegate.walletM) {
        return;
    }
    if ((!self.keyword||!self.keyword.length)&&!self.isHideSmall) {
        self.showArr = kAppDelegate.walletM.assetViews;
        return;
    }
    NSMutableArray *tempArr = @[].mutableCopy;
    for (CoinItemModel *item in kAppDelegate.walletM.assetViews) {
        if (self.keyword && self.keyword.length) {
            if (![[item.name lowercaseString] containsString:[self.keyword lowercaseString]]) {
                continue;;
            }
        }
        if (self.isHideSmall && item.balance <= 0) {
            continue;
        }
        [tempArr addObject:item];
    }
    self.showArr = tempArr;
}

@end
